var app = new Vue({
    el: ".box",
    data: {
        dynamicValidateForm: {
            domains: [{
                SMTP_name: "",//smtp服务器地址
                SMTP_port: "",//smtp服务器端口
                email_pos: "",//邮箱密码
                email_name: ""//邮箱别名
            }],
            email: ''//邮箱地址
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var d = this.dynamicValidateForm.domains[0];
                    var datas = {
                        head: 30000,
                        token: (sessionStorage.getItem('token')),
                        SMTP_name: d.SMTP_name,
                        SMTP_port: d.SMTP_port,
                        email_pos: d.email_pos,
                        email_name: d.email_name,
                        email: this.dynamicValidateForm.email
                    };
                    set_email(datas);
                } else {
                    this.$message({
                        message: '请根据提示填写信息',
                        type: 'warning'
                    });
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
});

set_email({ head: 30001, token: (sessionStorage.getItem('token')) });
function set_email(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/email.php',
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify(obj)
    }).then(function (res) {
        if (res.data.code == '200') {
            loading.close();
            var d = app.dynamicValidateForm.domains[0];
            if (obj.head == 30001) {
                var da = res.data.data[0];
                d.SMTP_name = da.SMTP_name;
                d.SMTP_port = da.SMTP_port;
                d.email_pos = da.email_pos;
                d.email_name = da.email_name;
                app.dynamicValidateForm.email = da.email;
            } else {
                app.$message({
                    message: '操作成功~',
                    type: 'success'
                });
            };
        } else {
            window.location.href = res.data.href || '../index.html';
        };
    }).catch(function (res) {
        console.log(res);
    });
};